//class ListBase
package baidu.ui.controls
{
    import baidu.ui.controls.list.*;
    import baidu.ui.core.*;
    import baidu.ui.data.*;
    import baidu.ui.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class ListBase extends baidu.ui.core.BUI
    {
        public function ListBase()
        {
            this._selectedIndices = [];
            this.rendererStyles = {};
            this.updatedRendererStyles = {};
            this.preChangeItems = [];
            this.activeCells = [];
            this.availableCells = [];
            this.renderedItems = new flash.utils.Dictionary(true);
            this.invalidItems = new flash.utils.Dictionary(true);
            super();
            if (this.dataProvider == null)
            {
                this.dataProvider = new baidu.ui.data.DataProvider();
            }
            return;
        }

        public override function get classStyles():Object
        {
            return mergeStyles(super.classStyles, defaultStyles);
        }

        public function get renderer():*
        {
            return getStyle("renderer");
        }

        public function set renderer(arg1:*):void
        {
            setStyle("renderer", arg1);
            return;
        }

        public function get dataProvider():baidu.ui.data.DataProvider
        {
            return this._dataProvider;
        }

        public function set dataProvider(arg1:baidu.ui.data.DataProvider):void
        {
            if (this._dataProvider != null)
            {
                this._dataProvider.removeEventListener(baidu.ui.events.DataChangeEvent.DATA_CHANGE, this.handleDataChange);
                this._dataProvider.removeEventListener(baidu.ui.events.DataChangeEvent.PRE_DATA_CHANGE, this.handlePreDataChange);
            }
            this._dataProvider = arg1;
            this._dataProvider.addEventListener(baidu.ui.events.DataChangeEvent.DATA_CHANGE, this.handleDataChange, false, 0, true);
            this._dataProvider.addEventListener(baidu.ui.events.DataChangeEvent.PRE_DATA_CHANGE, this.handlePreDataChange, false, 0, true);
            return;
        }

        public function get length():uint
        {
            return this._dataProvider.length;
        }

        public function get multiple():Boolean
        {
            return this._multiple;
        }

        public function set multiple(arg1:Boolean):void
        {
            if (arg1 == this._multiple)
            {
                return;
            }
            this._multiple = arg1;
            if (!arg1 && this._selectedIndices.length > 1)
            {
                this._selectedIndices = [this._selectedIndices.pop()];
                invalidate(baidu.ui.core.Invalidation.DATA);
            }
            return;
        }

        public function get selectable():Boolean
        {
            return this._selectable;
        }

        public function set selectable(arg1:Boolean):void
        {
            if (arg1 == this._selectable)
            {
                return;
            }
            if (!arg1)
            {
                this.selectedIndices = [];
            }
            this._selectable = arg1;
            return;
        }

        public function get selectedIndex():int
        {
            return this._selectedIndices.length != 0 ? this._selectedIndices[(this._selectedIndices.length - 1)] : -1;
        }

        public function set selectedIndex(arg1:int):void
        {
            this.selectedIndices = arg1 != -1 ? [arg1] : null;
            return;
        }

        public function set selectedIndices(arg1:Array):void
        {
            if (!this._selectable)
            {
                return;
            }
            this._selectedIndices = arg1 != null ? arg1.concat() : [];
            if (!this._multiple && this._selectedIndices.length > 1)
            {
                this._selectedIndices = [this._selectedIndices.pop()];
            }
            invalidate(baidu.ui.core.Invalidation.SELECTED);
            return;
        }

        public function get selectedIndices():Array
        {
            return this._selectedIndices.concat();
        }

        public function get selectedItem():Object
        {
            return this._selectedIndices.length != 0 ? this._dataProvider.getItemAt(this.selectedIndex) : null;
        }

        public function set selectedItem(arg1:Object):void
        {
            var loc1:*;
            loc1 = this._dataProvider.getItemIndex(arg1);
            this.selectedIndex = loc1;
            return;
        }

        public function get selectedItems():Array
        {
            var loc1:*;
            loc1 = [];
            var loc2:*;
            loc2 = 0;
            while (loc2 < this._selectedIndices.length)
            {
                loc1.push(this._dataProvider.getItemAt(this._selectedIndices[loc2]));
                loc2 = (loc2 + 1);
            }
            return loc1;
        }

        public function set selectedItems(arg1:Array):void
        {
            var loc3:*;
            loc3 = 0;
            if (arg1 == null)
            {
                this.selectedIndices = null;
                return;
            }
            var loc1:*;
            loc1 = [];
            var loc2:*;
            loc2 = 0;
            while (loc2 < arg1.length)
            {
                if ((loc3 = this._dataProvider.getItemIndex(arg1[loc2])) != -1)
                {
                    loc1.push(loc3);
                }
                loc2 = (loc2 + 1);
            }
            this.selectedIndices = loc1;
            return;
        }

        public function itemToCell(arg1:Object):baidu.ui.controls.list.IListCell
        {
            var loc1:*;
            loc1 = null;
            var loc2:*;
            loc2 = null;
            if (arg1 != null)
            {
                var loc3:*;
                loc3 = 0;
                var loc4:*;
                loc4 = this.activeCells;
                for (loc1 in loc4)
                {
                    loc2 = this.activeCells[loc1] as baidu.ui.controls.list.IListCell;
                    if (loc2.data != arg1)
                    {
                        continue;
                    }
                    return loc2;
                }
            }
            return null;
        }

        public function clearSelection():void
        {
            this.selectedIndex = -1;
            return;
        }

        public function addItem(arg1:Object):void
        {
            this._dataProvider.addItem(arg1);
            this.invalidateList();
            return;
        }

        public function addItemAt(arg1:Object, arg2:uint):void
        {
            this._dataProvider.addItemAt(arg1, arg2);
            this.invalidateList();
            return;
        }

        public function removeAll():void
        {
            this._dataProvider.removeAll();
            return;
        }

        public function getItemAt(arg1:uint):Object
        {
            return this._dataProvider.getItemAt(arg1);
        }

        public function removeItem(arg1:Object):Object
        {
            return this._dataProvider.removeItem(arg1);
        }

        public function removeItemAt(arg1:uint):Object
        {
            return this._dataProvider.removeItemAt(arg1);
        }

        public function replaceItemAt(arg1:Object, arg2:uint):Object
        {
            return this._dataProvider.replaceItemAt(arg1, arg2);
        }

        public function sortItems(... rest):*
        {
            return this._dataProvider.sort.apply(this._dataProvider, rest);
        }

        public function sortItemsOn(arg1:String, arg2:Object=null):*
        {
            return this._dataProvider.sortOn(arg1, arg2);
        }

        public function isItemSelected(arg1:Object):Boolean
        {
            return this.selectedItems.indexOf(arg1) > -1;
        }

        public function scrollToSelected():void
        {
            this.scrollToIndex(this.selectedIndex);
            return;
        }

        public function scrollToIndex(arg1:int):void
        {
            return;
        }

        public function getRendererStyle(arg1:String, arg2:int=-1):Object
        {
            return this.rendererStyles[arg1];
        }

        public function setRendererStyle(arg1:String, arg2:Object, arg3:uint=0):void
        {
            if (this.rendererStyles[arg1] == arg2)
            {
                return;
            }
            this.updatedRendererStyles[arg1] = arg2;
            this.rendererStyles[arg1] = arg2;
            invalidate(baidu.ui.core.Invalidation.RENDERER_STYLES);
            return;
        }

        public function clearRendererStyle(arg1:String, arg2:int=-1):void
        {
            delete this.rendererStyles[arg1];
            this.updatedRendererStyles[arg1] = null;
            invalidate(baidu.ui.core.Invalidation.RENDERER_STYLES);
            return;
        }

        protected override function initUI():void
        {
            super.initUI();
            return;
        }

        protected override function draw():void
        {
            super.draw();
            return;
        }

        protected function drawList():void
        {
            return;
        }

        protected function drawLayout():void
        {
            return;
        }

        public function invalidateList():void
        {
            this._invalidateList();
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        protected function _invalidateList():void
        {
            this.availableCells = [];
            while (this.activeCells.length > 0)
            {
                this.container.removeChild(this.activeCells.pop() as flash.display.DisplayObject);
            }
            return;
        }

        public function invalidateItem(arg1:Object):void
        {
            if (this.renderedItems[arg1] == null)
            {
                return;
            }
            this.invalidItems[arg1] = true;
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        public function invalidateItemAt(arg1:uint):void
        {
            var loc1:*;
            loc1 = this._dataProvider.getItemAt(arg1);
            if (loc1 != null)
            {
                this.invalidateItem(loc1);
            }
            return;
        }

        protected function handleDataChange(arg1:baidu.ui.events.DataChangeEvent):void
        {
            var loc4:*;
            loc4 = 0;
            var loc1:*;
            loc1 = arg1.startIndex;
            var loc2:*;
            loc2 = arg1.endIndex;
            var loc3:*;
            loc3 = arg1.changeType;
            var loc5:*;
            loc5 = loc3;
            switch (loc5)
            {
                case baidu.ui.events.DataChangeType.ADD:
                {
                    loc4 = 0;
                    while (loc4 < this._selectedIndices.length)
                    {
                        if (this._selectedIndices[loc4] >= loc1)
                        {
                            this._selectedIndices[loc4] = this._selectedIndices[loc4] + loc1 - loc2 + 1;
                        }
                        loc4 = (loc4 + 1);
                    }
                    break;
                }
                case baidu.ui.events.DataChangeType.INVALIDATE:
                {
                    loc4 = 0;
                    while (loc4 < arg1.items.length)
                    {
                        this.invalidateItem(arg1.items[loc4]);
                        loc4 = (loc4 + 1);
                    }
                    break;
                }
                case baidu.ui.events.DataChangeType.INVALIDATE_ALL:
                {
                    this.clearSelection();
                    this.invalidateList();
                    break;
                }
                case baidu.ui.events.DataChangeType.REMOVE:
                {
                    loc4 = 0;
                    while (loc4 < this._selectedIndices.length)
                    {
                        if (this._selectedIndices[loc4] >= loc1)
                        {
                            if (this._selectedIndices[loc4] <= loc2)
                            {
                                delete this._selectedIndices[loc4];
                            }
                            else
                            {
                                this._selectedIndices[loc4] = this._selectedIndices[loc4] - (loc1 - loc2 + 1);
                            }
                        }
                        loc4 = (loc4 + 1);
                    }
                    break;
                }
                case baidu.ui.events.DataChangeType.REMOVE_ALL:
                {
                    this.clearSelection();
                    break;
                }
                case baidu.ui.events.DataChangeType.REPLACE:
                {
                    break;
                }
                case baidu.ui.events.DataChangeType.SORT:
                {
                    this.selectedItems = this.preChangeItems;
                    this.preChangeItems = null;
                    break;
                }
            }
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        protected function handlePreDataChange(arg1:baidu.ui.events.DataChangeEvent):void
        {
            var loc1:*;
            loc1 = arg1.changeType;
            switch (loc1)
            {
                case baidu.ui.events.DataChangeType.ADD:
                case baidu.ui.events.DataChangeType.INVALIDATE:
                case baidu.ui.events.DataChangeType.INVALIDATE_ALL:
                case baidu.ui.events.DataChangeType.REMOVE:
                case baidu.ui.events.DataChangeType.REMOVE_ALL:
                case baidu.ui.events.DataChangeType.REPLACE:
                {
                    break;
                }
                default:
                {
                    this.preChangeItems = this.selectedItems;
                    break;
                }
            }
            return;
        }

        protected function handleCellRendererMouseEvent(arg1:flash.events.MouseEvent):void
        {
            var loc1:*;
            loc1 = arg1.target as baidu.ui.controls.list.IListCell;
            var loc2:*;
            loc2 = arg1.type != flash.events.MouseEvent.ROLL_OVER ? baidu.ui.events.ListEvent.ITEM_ROLL_OUT : baidu.ui.events.ListEvent.ITEM_ROLL_OVER;
            dispatchEvent(new baidu.ui.events.ListEvent(loc2, false, false, loc1.listData.col, loc1.listData.row, loc1.listData.index, loc1.data));
            return;
        }

        protected function handleCellChange(arg1:flash.events.Event):void
        {
            var loc1:*;
            loc1 = arg1.currentTarget as baidu.ui.controls.list.IListCell;
            var loc2:*;
            loc2 = loc1.listData.index;
            this._dataProvider.invalidateItemAt(loc2);
            return;
        }

        protected function handleCellClick(arg1:flash.events.MouseEvent):void
        {
            var loc4:*;
            loc4 = 0;
            var loc5:*;
            loc5 = 0;
            if (!_enabled)
            {
                return;
            }
            var loc1:*;
            loc1 = arg1.currentTarget as baidu.ui.controls.list.IListCell;
            var loc2:*;
            loc2 = loc1.listData.index;
            if (!dispatchEvent(new baidu.ui.events.ListEvent(baidu.ui.events.ListEvent.ITEM_CLICK, false, true, loc1.listData.col, loc1.listData.row, loc2, loc1.data)) || !this._selectable)
            {
                return;
            }
            var loc3:*;
            loc3 = this.selectedIndices.indexOf(loc2);
            if (this._multiple)
            {
                if (arg1.shiftKey)
                {
                    loc5 = this._selectedIndices.length > 0 ? this._selectedIndices[0] : loc2;
                    this._selectedIndices = [];
                    if (loc5 > loc2)
                    {
                        loc4 = loc5;
                        while (loc4 >= loc2)
                        {
                            this._selectedIndices.push(loc4);
                            loc4 = (loc4 - 1);
                        }
                    }
                    else
                    {
                        loc4 = loc5;
                        while (loc4 <= loc2)
                        {
                            this._selectedIndices.push(loc4);
                            ++loc4;
                        }
                    }
                    this.caretIndex = loc2;
                }
                else
                {
                    if (arg1.ctrlKey)
                    {
                        if (loc3 == -1)
                        {
                            loc1.selected = true;
                            this._selectedIndices.push(loc2);
                        }
                        else
                        {
                            loc1.selected = false;
                            this._selectedIndices.splice(loc3, 1);
                        }
                        this.caretIndex = loc2;
                    }
                    else
                    {
                        this._selectedIndices = [loc2];
                        this.caretIndex = loc6 = loc2;
                        this.lastCaretIndex = loc6;
                    }
                }
            }
            else
            {
                if (loc3 != -1)
                {
                    return;
                }
                loc1.selected = true;
                this._selectedIndices = [loc2];
                var loc6:*;
                this.caretIndex = loc6 = loc2;
                this.lastCaretIndex = loc6;
            }
            dispatchEvent(new flash.events.Event(flash.events.Event.CHANGE));
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        protected function handleCellDoubleClick(arg1:flash.events.MouseEvent):void
        {
            if (!_enabled)
            {
                return;
            }
            var loc1:*;
            loc1 = arg1.currentTarget as baidu.ui.controls.list.IListCell;
            var loc2:*;
            loc2 = loc1.listData.index;
            dispatchEvent(new baidu.ui.events.ListEvent(baidu.ui.events.ListEvent.ITEM_DOUBLE_CLICK, false, true, loc1.listData.col, loc1.listData.row, loc2, loc1.data));
            return;
        }

        protected function updateRendererStyles():void
        {
            var loc4:*;
            loc4 = null;
            var loc1:*;
            loc1 = this.availableCells.concat(this.activeCells);
            var loc2:*;
            loc2 = loc1.length;
            var loc3:*;
            loc3 = 0;
            while (loc3 < loc2)
            {
                if (loc1[loc3].setStyle != null)
                {
                    var loc5:*;
                    loc5 = 0;
                    var loc6:*;
                    loc6 = this.updatedRendererStyles;
                    for (loc4 in loc6)
                    {
                        loc1[loc3].setStyle(loc4, this.updatedRendererStyles[loc4]);
                    }
                    loc1[loc3].drawNow();
                }
                loc3 = (loc3 + 1);
            }
            this.updatedRendererStyles = {};
            return;
        }


        {
            defaultStyles = {"renderer":baidu.ui.controls.list.ListCell, "gap":0};
        }

        protected var _selectedIndices:Array;

        protected var _selectable:Boolean=true;

        protected var _multiple:Boolean=false;

        protected var _dataProvider:baidu.ui.data.DataProvider;

        protected var rendererStyles:Object;

        protected var updatedRendererStyles:Object;

        protected var preChangeItems:Array;

        protected var caretIndex:int=-1;

        protected var lastCaretIndex:int=-1;

        protected var activeCells:Array;

        protected var availableCells:Array;

        protected var renderedItems:flash.utils.Dictionary;

        protected var invalidItems:flash.utils.Dictionary;

        protected var container:flash.display.Sprite;

        public static var defaultStyles:Object;
    }
}


